#!/usr/bin/env bash

mruby_install()
{
  \typeset result temp_flags

  __rvm_check_for_bison # && Run like hell...
  if __rvm_check_for_bison
  then true
  else
    result=$?
    rvm_log "Bison required but not found. Halting."
    return $result
  fi

  __rvm_setup_compile_environment "${rvm_ruby_string}"

  true ${rvm_ruby_selected_flag:=0}

  \typeset directory db_configure_flags
  \typeset -a autoconf_flags

  (( rvm_ruby_selected_flag )) || __rvm_select

  rvm_log "Installing Ruby from source to: $rvm_ruby_home, this may take a while depending on your cpu(s)..."
  __rvm_cd "${rvm_src_path}"

  if __rvm_fetch_ruby
  then true
  else
    result=$?
    rvm_error "There has been an error fetching the ruby interpreter. Halting the installation."
    return $result
  fi

  if __rvm_cd "${rvm_src_path}/$rvm_ruby_string"
  then true
  else
    result=$?
    rvm_error "Source directory is missing.  \nDid the download or extraction fail?  \nHalting the installation."
    return $result
  fi

  if [[ -d "${rvm_path}/usr" ]]
  then
    __rvm_add_to_path prepend "${rvm_path}/usr/bin"
    builtin hash -r
  fi

  if __rvm_apply_patches
  then true
  else
    result="$?"
    rvm_error "There has been an error applying the specified patches. Halting the installation."
    return $result
  fi

  if __rvm_log_command "make" "$rvm_ruby_string - #compiling" ${rvm_ruby_make:-__rvm_make} "${rvm_make_flags[@]}"
  then true
  else
    result=$?
    rvm_error "There has been an error while running make. Halting the installation."
    return $result
  fi

  \command \mkdir -p "$rvm_ruby_home/"
  \command \mkdir -p "$rvm_ruby_gem_home/bin"
  \command \mkdir -p "$rvm_ruby_global_gems_path/bin"

  [[ -n "${rvm_ruby_make_install:-}" ]] || rvm_ruby_make_install=( __rvm_cp -Rf "${rvm_src_path}/$rvm_ruby_string"/{bin,mrblib,include} "$rvm_ruby_home/" )

  __rvm_log_command "install" "$rvm_ruby_string - #installing" "${rvm_ruby_make_install[@]}" ||
  {
    result=$?
    rvm_error "There has been an error while running make install. Halting the installation."
    return $result
  }

  \typeset __bin_file __target_file
  for __bin_file in "$rvm_ruby_home/bin"/m*
  do
    __bin_file="${__bin_file##*/}"
    __target_file="${__bin_file#m}"
    ln -s "${__bin_file}" "$rvm_ruby_home/bin/${__target_file}"
  done

  __rvm_ruby_config_save "$rvm_ruby_home/bin/ruby"
  __rvm_fetch_ruby_cleanup || return $?

  rvm_log "Install of $rvm_ruby_string - #complete "
}
